var priorityFilter = new Array();
var stateFilter = new Array();
var loggerFilter = new Array();
var fixerFilter = new Array();
var tagFilter = new Array();
var fixerBugs = {};
var loggerHeader = 0;
var fixerHeader = 0;

function init() {
	var oTable = document.getElementsByTagName("table")[0];
	var arrTr = oTable.getElementsByTagName("tr");
	var o = null;
	var p1 = 0, p2 = 0, p3 = 0, p4 = 0;
	var newNum = 0, fixedNum = 0, reviewdNum = 0;
	
	var arrTh = arrTr[0].getElementsByTagName("th");
	for(var i = 0; i < arrTh.length; i++) {
		if (arrTh[i].firstChild.innerHTML == "Logger") {
			loggerHeader = i;
		} else if (arrTh[i].firstChild.innerHTML == "Fixer") {
			fixerHeader = i;
		}
	}
	
	for(var i = 1; i < arrTr.length; i++) {
		var oTr = arrTr[i];
		var arrTd = oTr.getElementsByTagName("td");
		
		var summary = arrTd[3].firstChild.innerHTML;
		var tags = getSummaryTags(summary);
		for(var idx = 0; idx < tags.length;  idx++) {
			if(!containsValue(tagFilter, tags[idx])) {
				tagFilter.push(tags[idx]);
			}
		}

		if(arrTd[4].innerHTML == "1") {
			p1 ++;
		} else if(arrTd[4].innerHTML == "2") {
			p2 ++;
		} else if(arrTd[4].innerHTML == "3") {
			p3 ++;
		} else if(arrTd[4].innerHTML == "4") {
			p4 ++;
		}
		
		var state = arrTd[6].innerHTML;
		if (state != null && state != "" && !containsValue(stateFilter, state)) {
			stateFilter.push(state);
		}
		
		var loggerNode = arrTd[loggerHeader];
		if(loggerNode != null) {
			var logger = loggerNode.firstChild.innerHTML;
			if (logger != null && logger != "" && !containsValue(loggerFilter, logger)){
				loggerFilter.push(logger);
			}
		}
		
		var fixerNode = arrTd[fixerHeader];
		if(fixerNode != null) {
			var fixer = fixerNode.firstChild.innerHTML;
			if (fixer != null && fixer != "" && !containsValue(fixerFilter, fixer)){
				fixerFilter.push(fixer);
			}
			if(fixer == null || fixer == "") {
				fixer = "Not Assign";
			}
			if(fixerBugs[fixer] == null) {
				fixerBugs[fixer] = 1;
			} else {
				fixerBugs[fixer] = fixerBugs[fixer] + 1;
			}
		}
	}
	
	var ps = "&nbsp;";
	ps += "<div id='divFilterTypes' >";
	ps += "<table border='0'>";
	ps += "<tr><td>Priority</td>";
	ps += "<td><div id='divPriorityFilter' >";
	ps += "<input type='checkbox' name='priority' value='1' checked='true' onclick=\"filterByPriority(this, '1')\">P1</input> &nbsp;";
	ps += "<input type='checkbox' name='priority' value='2' checked='true' onclick=\"filterByPriority(this, '2')\">P2</input> &nbsp;";
	ps += "<input type='checkbox' name='priority' value='3' checked='true' onclick=\"filterByPriority(this, '3')\">P3</input> &nbsp;";
	ps += "<input type='checkbox' name='priority' value='4' checked='true' onclick=\"filterByPriority(this, '4')\">P4</input> &nbsp;";
	ps += "</div></td></tr>";
	
	ps += "<tr><td>State</td>";
	ps += "<td><div id='divStateFilter'>";
	for (var i = 0; i < stateFilter.length; i++) {
		ps += "<input type='checkbox' name='state' value='"+stateFilter[i]+"' checked='true' onclick=\"filterByState(this, '"+stateFilter[i]+"')\">"+stateFilter[i]+"</input> &nbsp; ";	
	}
	ps += "</div></td></tr>";
	
	ps += "<tr><td>Tag</td>";
	ps += "<td><div id='divTag'><select id='tag' name='tag' onchange='filterByTag(this)'>";
	ps += "<option value=''>All</option>";
	tagFilter = tagFilter.sort();
	for (var i = 0; i < tagFilter.length; i++) {
		var tag = tagFilter[i];
		ps += "<option value='"+tag+"'>"+tag+"</option>";
	}
	ps += "<option value='Others'>Others</option>";
	ps += "</select>";
	ps += "</div></td></tr>";
	tagFilter.push("Others");
	
	ps += "<tr><td>Logger</td>";
	ps += "<td><div id='divLogger'><select id='logger' name='logger' onchange='filterByLogger(this)'>";
	ps += "<option value='*'>All</option>";
	loggerFilter = loggerFilter.sort();
	for (var i = 0; i < loggerFilter.length; i++) {
		var logger = loggerFilter[i];
		ps += "<option value='"+logger+"'>"+logger+"</option>";
	}
	ps += "<option value=''>(Empty)</option>";
	ps += "</select>";
	ps += "</div></td></tr>";
	loggerFilter.push('*');
	loggerFilter.push('');
	
	ps += "<tr><td>Fixer</td>";
	ps += "<td><div id='divFixer'><select id='fixer' name='fixer' onchange='filterByFixer(this)'>";
	ps += "<option value='*'>All</option>";
	fixerFilter = fixerFilter.sort();
	for (var i = 0; i < fixerFilter.length; i++) {
		var fixer = fixerFilter[i];
		ps += "<option value='"+fixer+"'>"+fixer+"</option>";
	}
	ps += "<option value=''>(Empty)</option>";
	ps += "</select>";
	ps += "</div></td></tr>";
	fixerFilter.push('*');
	fixerFilter.push('');
	
	ps += "</table>";
	ps += "<input type='hidden' name='reload' value='true'/>";
	ps += "<input type='submit' value='reload' onclick='reloadPage()'/>";
	ps += "</div>"
	ps += "<a href='#' onclick='switchFilterStyle()'/>show/hide filter</a>";
	
	//$("form")[0].innerHTML = '';
	document.getElementById('divFilter').innerHTML = ps;
	
	var sChart = '';
	sChart += "<a href='#' onclick='switchChartStyle()'/>show/hide chart</a>";
	sChart += "<div id='divPieChart'></div>";
	sChart += "<div id='divBarChart'></div>";
	document.getElementById('divChart').innerHTML = sChart;
	
	$("input:button", document.body).each(function(i) {
			if($(this).attr("value") == 'Save' 
				|| $(this).attr("value") == 'Print'
				|| $(this).attr("value") == 'Export') {
				$(this).css('display', 'none');
			}
		});
	
	priorityFilter[0] = 1;priorityFilter[1] = 2;priorityFilter[2] = 3;priorityFilter[3] = 4;

	preFilter();
	filter();

	setTimeout("autorefresh()",10 * 60 * 1000);
}
function preFilter() {
	if(reload == false) {
		return;
	}
	priorityFilter = lastPriorityFilter;
	$("input:checkbox", $("#divPriorityFilter")).each(function(i) {
			if(!containsValue(priorityFilter, $(this).attr("value"))) {
				$(this).attr("checked", "");
			}
		});
	stateFilter = lastStateFilter;
	$("input:checkbox", $("#divStateFilter")).each(function(i) {
			if(!containsValue(stateFilter, $(this).attr("value"))) {
				$(this).attr("checked", "");
			}
		});
	if(lastTagFilter[0] != "") {
		tagFilter = lastTagFilter;
		$("#tag option").each(function (i) {
				if($(this).attr("value") == tagFilter[0]) {
					$(this).attr('selected', 'true');
				}
			});
	}
	if(lastLoggerFilter[0] != "*") {
		loggerFilter = lastLoggerFilter;
		$("#logger option").each(function (i) {
				if($(this).attr("value") == loggerFilter[0]) {
					$(this).attr('selected', 'true');
				}
			});
	}
	if(lastFixerFilter[0] != "*") {
		fixerFilter = lastFixerFilter;
		$("#fixer option").each(function (i) {
				if($(this).attr("value") == fixerFilter[0]) {
					$(this).attr('selected', 'true');
				}
			});
	}
}
function filter() {
	var count = 0;
	$('tr', $('table')[1]).each(function (i) {
		if (i > 0) {
			var summary = $("td", $(this))[3].firstChild.innerHTML;
			var tags = getSummaryTags(summary);
			if(tags.length == 0) {
				tags.push("Others");
			}
			var displayByTag = false;
			if(tagFilter.length == 0 || containsOneOfValues(tagFilter, tags)) {
				displayByTag = true;
			}
			
			var priority = $("td", $(this))[4].innerHTML;
			var state = $("td", $(this))[6].innerHTML;
			var logger = $("td", $(this))[loggerHeader].firstChild.innerHTML;
			logger = (logger == null) ? '' : logger;
			var fixer = $("td", $(this))[fixerHeader].firstChild.innerHTML;
			fixer = (fixer == null) ? '' : fixer;
			if (containsValue(priorityFilter, priority) &&
				containsValue(stateFilter, state) &&
				containsValue(loggerFilter, logger) &&	
				containsValue(fixerFilter, fixer) &&
				displayByTag) {
				$(this).css("display", "");
				count++;
			} else {
				$(this).css("display", "none");
			}
		}
	});
	$("h1")[0].innerHTML = count + " Problems Found";
}
function filterByTag(obj) {
	var tag = obj.value;
	tagFilter = new Array();
	if(tag != '') {
		tagFilter.push(tag);
	}
	filter();
}
function filterByLogger(obj) {
	var logger = obj.value;
	loggerFilter = new Array();
	if(logger == '*') {
		var options = obj.options;
		for (var i = 0; i < options.length; i++) {
			loggerFilter.push(options[i].value);
		}
	} else {
		loggerFilter.push(logger);
	}
	filter();
}
function filterByFixer(obj) {
	var fixer = obj.value;
	fixerFilter = new Array();
	if(fixer == '*') {
		var options = obj.options;
		for (var i = 0; i < options.length; i++) {
			fixerFilter.push(options[i].value);
		}
	} else {
		fixerFilter.push(fixer);
	}
	filter();
}
function filterByPriority(obj, priority) {
	if (obj.checked) {
		if (containsValue(priorityFilter, priority)) {
			return;
		}
		priorityFilter.push(priority);
	} else {
		for (var i = 0; i < priorityFilter.length; i++) {
			if (priorityFilter[i] == priority) {
				priorityFilter.splice(i, 1);
			}
		}
	}
	filter();
}

function filterByState(obj, state) {
	if (obj.checked) {
		if (containsValue(stateFilter, state)) {
			return;
		}
		stateFilter.push(state);
	} else {
		for (var i = 0; i < stateFilter.length; i++) {
			if (stateFilter[i] == state) {
				stateFilter.splice(i, 1);
			}
		}
	}
	filter();
}
function getSummaryTags(summary) {
	var tags = new Array();
	var arr = summary.split("[");
	if(arr.length > 1) {
		for(var idx in arr) {
			var s = arr[idx];
			if(s.indexOf("]") > 0) {
				s = s.substring(0, s.indexOf("]")).trim();
				tags.push(s);
			}
		}
	}
	return tags;
}
function switchFilterStyle() {
	if($('#divFilterTypes').css('display') == 'none') {
		if(!chart_flag) {
			drawChart();
		}
		$('#divFilterTypes').css('display', 'block');
	} else {
		$('#divFilterTypes').css('display', 'none');
	}
}
function containsValue(arr, obj) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == obj) {
			return true;
		}
	}
	return false;
}
/**
 * Check arr1 contains any one of arr2.
 */
function containsOneOfValues(arr1, arr2) {
	for (var i = 0; i < arr2.length; i++) {
		if(containsValue(arr1, arr2[i])) {
			return true
		}
	}
	return false;
}

function printArray(arr) {
	var s = "";
	for (var i = 0; i < arr.length; i++) {
		if (i != 0) {
			s += "\n";
		}
		s += arr[i];
	}
	alert(s);
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function goorder(order) {
	// do nothing
}
function reloadPage() {
	document.forms[0].action='';
	document.forms[0].submit();
}
function autorefresh() {
	document.forms[0].action='';
	document.forms[0].submit();
}
$(document).ready(init);
$(document).dblclick(function () {
	if($('#divFilterTypes').css('display') == 'block') {
		$('#divFilterTypes').css('display', 'none');
	}
});
